<html>
<head>
<title>锁具报废记录</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<link rel="shortcut icon" href="resources/images/lock.png" type="image/x-icon">
<link rel="stylesheet" href="resources/css/stylemain.css">
<link rel="stylesheet" href="resources/layui/css/layui.css">

<link rel="stylesheet" href="resources/layui/css/treeTable.css">
<link rel="stylesheet" href="resources/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<script src="resources/layui/layui.all.js"></script>
<script type="text/javascript" src="resources/js/jquery.js"></script>
<script type="text/javascript" src="resources/js/base64.js"></script>
</head>
<body>
<table style="margin-top:10px">
	<tr>
		<td>
			<button class="layui-btn layui-btn-sm" onclick="exportcap()"><i class="layui-icon layui-icon-export"></i> 导出</button>
		</td>		
		<td>
			<label style="margin-left:10px">所属机构:</label><input type="text" id="orgname" name="orgname" class="table_bar_tool" style="text-align: left;width:200px;" value="">
		</td>
		<td>
			<label style="margin-left:10px">识别码:</label><input type="text" id="locksn" class="table_bar_tool">
		</td>
		<td>
			<button class="layui-btn layui-btn-sm" onclick="doSearch()"><i class="layui-icon layui-icon-search"></i> 查询</button>
		</td>
	</tr>
</table>
<input id="org_id" type="text" name="org_id" style="display:none"></input>
<table class="layui-hide" id="tbl_log" lay-filter="tbl_log"></table>
<div id="page"></div>


<script type="text/javascript">
var table = layui.table;
var laypage = layui.laypage;
var _currpage = 1;
var _pageSize = 20;

var _exportcheck = "";


layui.use([], function(){	
	getData(_currpage,_pageSize);
	 
});

$('#ckpassword').on('keydown', function (event) {
    if (event.keyCode == 13) {
    	userckopt();
		layer.closeAll();
    }
});

function ckdialgo_auth(){
	layer.open({
		  type: 1 //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层） 
		  ,title: "复核" //标题
		  ,skin:"layui-layer-lan"   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
		  ,area: ['350px', '260px'] //宽度、高度
		  ,shade: 0.3
		  ,offset: 'auto'
		  ,anim:5 //弹出动画
		  ,shadeClose: false //弹框出现后，允许点击其它地方关闭
		  ,content: $("#dlgck")     //内容
		  ,btn: ['确定', '取消'] //只是为了演示
		  ,success:function(layero){
			     //var mask = $(".layui-layer-shade");
			     //mask.appendTo(layero.parent());
			     $('#ckname').focus();
			     //其中：layero是弹层的DOM对象
			}
		 ,yes: function(index, layero){
		    //按钮【按钮一】的回调
		    	userckopt();
	    		$("#dlgck").css('display','none');
			 	layer.close(index);
		    	
			 	 
		  }
		  ,btn2: function(index, layero){
		    	//按钮【按钮二】的回调
		    
		    	$("#dlgck").css('display','none'); 
		    	
		  }
		  ,cancel: function(){ 
			    //右上角关闭回调
			    $("#dlgck").css('display','none'); 
			    
		 }
	});
}


table.on('toolbar(test)', function(obj){
	//debugger;
	var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'search':
    	  doSearch();
      break;
      
    }
      
  });
  
function doSearch(){
	_currpage = 1;
	getData(_currpage,_pageSize);
}

function getData(pageNumber,pageSize){
	////debugger;
	//var page_Number = $('#dg').datagrid('options').pageNumber;
	//pageSize为datagrid的每页记录条数
	//var page_size = table_name.config.limit;
	var datastring = '{"pageNumber":'+pageNumber+',"pageSize":'+ pageSize + ',"orgname":"' + $("#orgname").val()+ '","locksn":"' + $("#locksn").val() + '","ckname":"","ckpassword":"","rname":"系统管理/日志管理/锁具报废记录"}';
	//EasyUILoad();
	//debugger;
	$.ajax({
		contentType: "application/json;charset=UTF-8",
		url : "getLockoffLogs.do?",
		type : "post",
		data : datastring,
		dataType : "json",
		success : function(msg) {
			//debugger;
			_exportcheck = msg.needcheck;
			tablerender(msg);
			page(msg);
			if(msg.status != "1"){
				layer.msg(msg.desc);
			}
			
		},
        error : function(e){
        	//dispalyEasyUILoad();
            alert(e.status);
            alert(e.responseText);
        }
	});
}

function tablerender(data){	
	table.render({
		  elem: '#tbl_log'
		  //,toolbar: '#toolbarDemo'
		  ,cellMinWidth: 50 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
		  ,cols: [[
			 {field:'locksn', title: '锁具识别码'}
			,{field:'locktype', title: '锁具类型', width:100, align: 'center', templet: '#locktypeTpl'}
		    ,{field:'oper_reals', title: '使用人'}
		    ,{field:'orgname', title: '所属机构'}
		    ,{field:'area', title: '使用区域'}
		    ,{field:'boxid', title: '使用箱号'}
		    ,{field:'offtime', title: '报废时间', width:180}
		    ,{field:'offuser', title: '操作人'}
		    ,{field:'descriptor', title: '备注'}
		  ]]
		  ,data:data.datalist
		  ,limit: data.pageSize
		  ,height: parent.iframeH - 110
		});		
}

function numpage(pageNumber, pageSize) {
	var datastring = '{"pageNumber":'+pageNumber+',"pageSize":'+ pageSize + ',"orgname":"' + $("#orgname").val() + '","locksn":"' + $("#locksn").val() + '","ckname":"","ckpassword":"","rname":"系统管理/日志管理/锁具报废记录"}';
	//EasyUILoad();
	//debugger;
	$.ajax({
		contentType: "application/json;charset=UTF-8",
		url : "getLockoffLogs.do?",
		type : "post",
		data : datastring,
		dataType : "json",
		success : function(msg) {
			//debugger;
			tablerender(msg);
		},
        error : function(e){
        	//dispalyEasyUILoad();
            alert(e.status);
            alert(e.responseText);
        }
	});
}

function page(data) {
    laypage.render({
        elem: 'page', //注意，这里的 page 是 ID，不用加 # 号
        count: data.totalCount, //数据总数，从服务端得到
        limit: data.pageSize,  // 每页条数
        limits: [10, 20, 30, 40, 50],
        layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
        curr: _currpage,
        jump: function (obj, first) {
            //console.log(obj)
            //obj包含了当前分页的所有参数，比如：
            //layer.msg(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            //console.log(obj.limit); //得到每页显示的条数

            //首次不执行
            if (!first) {
                //do something
                _currpage = obj.curr;
                _pageSize = obj.limit;
                numpage(obj.curr, obj.limit)  // 分页点击传参 
            }
        }
    });
}

function userckopt(){
	//debugger;
	var ckname = $('#ckname').val();
	if(ckname == ''){
		layer.msg('请填写用户账号!');
		return;
	}
	var ckpassword = $('#ckpassword').val();
	if(_curopt == "export"){
		var datastring = '{"ckname":"' + ckname + '","ckpassword":"' + $.base64.encode(ckpassword) + '","rname":"系统管理/日志管理/日志导出"}';	
		$.ajax({
			contentType: "application/json;charset=UTF-8",
			url : "optcheck.do?",
			type : "post",
			data : datastring,
			dataType : "json",
			success : function(msg) {
				//debugger;
				if(msg.status == "1"){
					$("#dlgck").css('display','none'); 
					layer.closeAll();
					var url = "lockofflog_download?ckname=" + ckname + "&ckpassword=" + ckpassword+"&orgname="+$("#orgname").val()+"&locksn="+$("#locksn").val();
					window.open(url);
				}else{
					layer.msg(msg.desc);
				}
				
			},
	        error : function(e){
	            alert(e.status);
	            alert(e.responseText);
	        }
		});
	}
	
	
}

function userckopt_cancel(){
	$('#ckname').val('');
	$('#ckpassword').val('');	
}

function exportcap(ckname,ckpassword){
	_curopt = "export";
	if(_exportcheck == "1"){
		$('#ckname').val('');
		$('#ckpassword').val('');
		ckdialgo_auth();
	}else{
		var url = "lockofflog_download?ckname=" + ckname + "&ckpassword=" + ckpassword+"&orgname="+$("#orgname").val()+"&locksn="+$("#locksn").val();
		window.open(url);
	}
	
}

function dateFormat(fmt, date) {
	var ret;
    var opt = {
        "Y+": date.getFullYear().toString(),        
        "m+": (date.getMonth() + 1).toString(),     
        "d+": date.getDate().toString(),           
        "H+": date.getHours().toString(),          
        "M+": date.getMinutes().toString(),        
        "S+": date.getSeconds().toString()         
    };
    for (var k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : pad(opt[k], ret[1].length));
        };
    };
    return fmt;
}

function myparser(s){
	if (!s) return new Date();

	var y,m,d;
	var ss = (s.split('-'));
	if(ss.length > 1){
		y = parseInt(ss[0],10);
		m = parseInt(ss[1],10);
		d = parseInt(ss[2],10);
	}else{
		y = parseInt(ss[0].substring(0,4),10);
		m = parseInt(ss[0].substring(4,6),10);
		d = parseInt(ss[0].substring(6,8),10);
	}
	
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	} else {
		return new Date();
	}
}

function pad(num, n) {  
    var len = num.toString().length;  
    while(len < n) {  
        num = "0" + num;  
        len++;  
    }  
    return num;  
}  

</script>
<script type="text/html" id="locktypeTpl">
  {{#  if(d.locktype == '1'){ }}	
    <span>主管锁</span>
  {{# }else if(d.locktype == '2'){ }}	
    <span>柜员锁</span>
  {{#  } else if(d.locktype == '3') { }}
    <span">调拨锁</span>
  {{#  } }}
</script>
</body>
</html>
